<?php

class Application_Form_Patient extends Zend_Form {

    public function __construct($formAction = "Enregistrer", $legende = 'Nouveau') {

        $this->setDecorators(array('FormElements', 'Form')); // supprime la balise <dl>

        $decorators = array(
            'ViewHelper',
            'Errors',
            array('Description', array('tag' => 'p', 'class' => 'description')),
            array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'input')), // 3
            array('Label', array('tag' => null, 'class' => '')), // 2
            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'clearfix')) // 1) exterieur
        );

        $noDecorators = array('ViewHelper');

        $id = new Zend_Form_Element_Hidden('id_patient');
        $id->setBelongsTo('Patient')
                ->addFilter('Int')
                ->addFilter('Null');

        $title = new Zend_Form_Element_Select('titre');
        $title->setLabel('Civilité')
                ->setBelongsTo('Patient')
                ->setAttribs(array('class' => 'span2'))
                ->setMultiOptions(array('' => '', 'mr' => 'Monsieur', 'mme' => 'Madame'))
                ->setRequired(true)->addValidator('NotEmpty', true);

        $nom = new Zend_Form_Element_Text('nom');
        $nom->setLabel('Nom')
                ->setBelongsTo('Patient')
                ->setRequired(true)
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $prenom = new Zend_Form_Element_Text('prenom');
        $prenom->setLabel('Prenom')
                ->setBelongsTo('Patient')
                ->setRequired(true)
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $naissance = new Zend_Form_Element_Text('date_naissance');
        $naissance->setLabel('Date de naissance')
                ->setBelongsTo('Patient')
                ->setAttribs(array('placeholder' => 'jj/mm/aaaa', 'style'=>'margin-right: 4px;'))
                ->setRequired(true)
                ->addValidator(new Zend_Validate_Date(array('format' => 'dd/MM/YYYY', 'locale' => 'fr')))
                ->setAttrib('class', 'span2');

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('eMail')
                ->setBelongsTo('Patient')
                ->setRequired(false)
                ->addFilter('Null')
                ->addValidator('EmailAddress', true)
                ->setAttrib('class', 'xlarge')
                ->addFilter('StringTrim');

        $numberSS = new Cab_Form_Element_numberSS('numberSS');
        $numberSS->setLabel('Numero SS')
                ->setBelongsTo('Patient')
                ->setRequired(true);

        // url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
        $urlBack = $this->getView()->url(array('controller' => 'patient', 'action' => 'index'), null, true);
        $cancel = new Zend_Form_Element_Button('cancel');
        $cancel->setLabel('Annuler')
                ->setAttrib('class', 'btn')
                ->setAttrib('type', 'reset')
                ->setDecorators(array('ViewHelper'))
                ->setAttrib('onclick', 'document.location =\'' . $urlBack . '\' ');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($formAction)
                ->setAttrib('id', 'submitbutton')
                ->setAttrib('class', 'btn primary');

        // Add Elements
        $this->addElements(array($id, $title, $nom, $prenom,  $naissance, $numberSS, $email, $cancel, $submit));

        // Subform Adresse
        $adresseSub = new Application_Form_SubForm_Adresse();

        $fiel_name = null;
        foreach ($adresseSub->getElements() as $name => $element) {

            if ($name != 'fax') {
                $this->addElement($element);
                $fiel_name[] = $name;
            }
        }


        $this->setElementDecorators(array('ViewHelper'), array('submit', 'cancel'));
        $this->setElementDecorators($decorators, array_merge(array('titre', 'prenom', 'nom', 'date_naissance', 'numberSS', 'email'), $fiel_name));
        $this->setElementDecorators($noDecorators, array('id_patient', 'id_adresse'));

        // Patient Group
        $this->addDisplayGroup(
                array_merge(array('titre', 'prenom', 'nom', 'date_naissance','date_label' ,'numberSS', 'email'), $fiel_name)
                , 'patientGroup', array('legend' => $legende .  ' Patient'));

        $contact = $this->getDisplayGroup('patientGroup');
        $contact->setDecorators(array(
            'FormElements',
            'Fieldset',
            array('HtmlTag', array('tag' => 'div', 'class' => 'span12', '_style' => 'width:40%;color:blue;')),
            array(array('rowspan' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))));

        // Buttons Group
        $this->addDisplayGroup(array('cancel', 'submit'), 'buttonGroupe');
        $this->buttonGroupe->removeDecorator('Label');
        $this->buttonGroupe->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'actions'))
        ));
    }

}

